package com.itheima.controller;

import com.itheima.MessageConstant;
import com.itheima.fegin.BlogWebFegin;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * sql搜索相关接口
 *
 * @author guifan
 * @version 1.0
 * @date 2020/7/13 17:37
 */

@RestController
@RequestMapping("/search")
@Api(tags = "SQL搜索相关接口")
public class SearchController {
    @Autowired
    private BlogWebFegin blogWebFegin;

    //通过作者搜索blog
    @GetMapping("/searchBlogByAuthor")
    @ApiOperation("根据作者获取相关的博客")
    public String searchBlogByAuthor(@RequestParam("author") String author, @RequestParam("currentPage") String currentPage, @RequestParam("pageSize") String pageSize) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("author", author);
            map.put("currentPage", currentPage);
            map.put("pageSize", pageSize);
            Map searchMap = blogWebFegin.elasticSearchBlog(map);
            return ResultUtil.result(200, searchMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.BLOG_SEARCHBLOGBYAUTHOR_FALL);
        }
    }

    //通过博客分类uid获取blog
    @GetMapping("/searchBlogBySort")
    @ApiOperation("根据分类获取相关的博客")
    public String searchBlogBySort(@RequestParam("blogSortUid") String blogSortUid, @RequestParam("currentPage") String currentPage, @RequestParam("pageSize") String pageSize) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("blogSortUid", blogSortUid);
            map.put("currentPage", currentPage);
            map.put("pageSize", pageSize);
            Map searchMap = blogWebFegin.elasticSearchBlog(map);
            return ResultUtil.result(200, searchMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.BLOG_SEARCHBLOGBYSORT_FALL);
        }
    }

    //通过博客uid获取blog
    @GetMapping("/searchBlogByTag")
    @ApiOperation("根据标签获取相关的博客")
    public String searchBlogByTag(@RequestParam("tagUid") String tagUid, @RequestParam("currentPage") String currentPage, @RequestParam("pageSize") String pageSize) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("tagUid", tagUid);
            map.put("currentPage", currentPage);
            map.put("pageSize", pageSize);
            Map searchMap = blogWebFegin.elasticSearchBlog(map);
            return ResultUtil.result(200, searchMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.BLOG_SEARCHBLOGBYTAG_FALL);
        }
    }

    //通过关键字搜索blog
    @GetMapping("/sqlSearchBlog")
    @ApiOperation("搜索Blog")
    public String sqlSearchBlog(@RequestParam("keyword") String keyword, @RequestParam("currentPage") String currentPage, @RequestParam("pageSize") String pageSize) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("keyword", keyword);
            map.put("currentPage", currentPage);
            map.put("pageSize", pageSize);
            Map searchMap = blogWebFegin.elasticSearchBlog(map);
            return ResultUtil.result(200, searchMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.BLOG_SQLSEARCHBLOG_FALL);
        }
    }



}
